package vno.fux.ui
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	
	import vno.fux.CompassDirection;
	import vno.fux.res.pool.BitmapAttacher;
	import vno.fux.res.pool.PicButtonResource;
	import vno.fux.res.pool.ResourcePool;

	public class PicButtonView extends BaseComponentView
	{
		private var _pic:BitmapAttacher;
		private var _disColorFilter:ColorMatrixFilter;
		public function PicButtonView(model_:BaseComponentModel, controller_:BaseComponentController)
		{
			super(model_, controller_);
			_pic = new BitmapAttacher;
			this.content = _pic;
			this.setRegPoint(CompassDirection.CENTER);
			_pic.addEventListener(BitmapAttacher.RECIEVE, recieveHandler);
			(ResourcePool.getInstance().uiRes.getRes(_model.resId) as PicButtonResource).button.attach(_pic);

			createDiscolorFilter();
			
			this.x = _model.pos.x;
			this.y = _model.pos.y;
			this.alpha = 0.9;
			this.addEventListener(MouseEvent.ROLL_OVER, enlarge);
			this.addEventListener(MouseEvent.ROLL_OUT, reset);
			this.addEventListener(MouseEvent.CLICK, (_controller as PicButtonController).handleClick);
		}
		
		private function createDiscolorFilter():void
		{
			var matrix:Array = new Array();
			matrix = matrix.concat([0.3,	0.59,	0.11,	0,	0]); // red
			matrix = matrix.concat([0.3,	0.59,	0.11,	0,	0]); // green
			matrix = matrix.concat([0.3,	0.59,	0.11,	0,	0]); // blue
			matrix = matrix.concat([0,		0,		0,		1,	0]); // alpha
			_disColorFilter = new ColorMatrixFilter(matrix);
		}
		private function recieveHandler(event:Event):void
		{
			refreshRegPoint();
		}
		
		private function reset(event:MouseEvent):void
		{
			this.scaleX = 1;
			this.scaleY = 1;

		}
		
		private function enlarge(event:MouseEvent):void
		{
			this.scaleX = 1.1;
			this.scaleY = 1.1;

		}
		override public function update(event:Event):void
		{
			var model:PicButtonModel = _model as PicButtonModel;
			if(model.soundChannel)
			{
				this.filters = null;
			}
			else
			{
				this.filters = [_disColorFilter];
			}	
		}
	}
}
